import pygame
import math
import os
import random
os.chdir("E:\\pygame\\demo")

# 初始化
pygame.init()
screen=pygame.display.set_mode((800,600))
pygame.display.set_caption("hit the airplane")
icon=pygame.image.load('ufo.png')
pygame.display.set_icon(icon)

# 基本要素
bg_img=pygame.image.load("bg.png")
player_img=pygame.image.load("player.png")

# 背景音乐
pygame.mixer.music.load("bg.wav")
pygame.mixer.music.play(-1)

# 创建射中音效
sound=pygame.mixer.Sound("exp.wav")



# 玩家信息
player_x=400
player_y=450
player_step=0
def move_player():
    global player_x
    player_x+=player_step
    if player_x>736:
        player_x=736
    if player_x<0:
        player_x=0

# 游戏结束
is_over=False
over_font=pygame.font.SysFont('simsunnsimsun',64)
def check_is_over():
    if is_over:
        text='Game Over'
        render=over_font.render(text,True,(255,0,0))
        screen.blit(render,(200,250))
        

# 敌人
class Enemy():
    def __init__(self):
        self.img=pygame.image.load("enemy.png")
        self.x=random.randint(10,400)
        self.y=random.randint(10,300)
        self.step=random.randint(1,2)
    def reset(self):
        self.x=random.randint(10,400)
        self.y=random.randint(10,300)
        
the_number_enemy=6
enemies=[]
for i in range(the_number_enemy):
    enemies.append(Enemy())   # 创建敌人列表
def move_enemy():
    global is_over
    for e in enemies:
        screen.blit(e.img,(e.x,e.y))  # 加载敌人至窗口
        e.x+=e.step
        if e.x>736 or e.x <0:
            e.step*=-1
            e.y+=40  # 触壁反弹+下沉
        if e.y>450:
            is_over=True
            print("Game Over")
            enemies.clear()

# 判断子弹和敌人的距离
def distance(e_x,e_y,b_x,b_y):
    a=e_x-b_x
    b=e_y-b_y
    return math.sqrt(a*a+b*b)

# 分数
score=0
font=pygame.font.SysFont("arial",32)
def show_score():
    text=f"Score:{score}"
    score_render=font.render(text,True,(0,255,0))
    screen.blit(score_render,(10,10))


# 子弹
class Bullet():
    def __init__(self):
        self.img=pygame.image.load("bullet.png")
        self.x=player_x+16
        self.y=player_y+10
        self.step=8
    def hit(self):
        global score
        for e in enemies:
            if distance(e.x,e.y,self.x,self.y)<30:
                sound.play() 
                score+=1
                e.reset()
                bullets.remove(self)
                print(score)
bullets=[]

# 显示子弹
def show_bullet():
    for b in bullets:
        screen.blit(b.img,(b.x,b.y))
        b.hit()
        b.y-=b.step
        if b.y<0:
            bullets.remove(b)





# 主函数
running=True
while running:
    screen.blit(bg_img,(0,0)) # 加载背景图片
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False
        elif event.type==pygame.KEYDOWN:
            if event.key==pygame.K_RIGHT:
                player_step=1
            elif event.key==pygame.K_LEFT:
                player_step=-1
            elif event.key==pygame.K_SPACE:
                bullets.append(Bullet())
        if event.type==pygame.KEYUP:
            player_step=0

    
    screen.blit(player_img,(player_x,player_y))  # 加载玩家
    move_player()  # 移动玩家
    move_enemy()
    show_bullet()
    check_is_over()
    show_score() 
    pygame.display.update()  # 更新
